<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Car extends Model
{
	const CREATED_AT = 'add_time';
    protected $table = 'jdc_cars';
    public $timestamps = false;

    /**
     * 获取汽车品牌
     */
    public function GetBrand() {
    	return $this->select('brand')->groupBy('brand')->get();
    }

    /**
     * 筛选汽车
     * @param Array $where 所有的查询条件和排序 
     * @param integer $page 默认每页的数据条数 
     */
    public function CarScreen($where,$page=20) {
        // 新车排序
    	$order_w = $where['order'];
		$order_wa = [
            // 按上架时间
    		'Time_1'=>['add_time','asc']
    		,'Time_2'=>['add_time','desc']
            // 按服务费
    		,'FirstPay_1'=>['first_pay','asc']
    		,'FirstPay_2'=>['first_pay','desc']
            // 月租金
    		,'MonthPay_1'=>['month_pay','asc']
            ,'MonthPay_2'=>['month_pay','desc']
            // 默认排序
    		,'preferentialRatio'=>['preferential_ratio','asc']
    	];
    	return $this->where(function($query)use($where){
            // 按品牌筛选
    		if(!empty($where['brand'])){
    			$brand_w = $where['brand'];
    			$query = $query->where('brand','=',$brand_w);
    		}
            // 按指导价区间筛选
    		if(!empty($where['guide_price'])){
    			$guide_w = $where['guide_price'];
    			if(is_array($guide_w)){
    				$query->whereBetween('guide_price',[$guide_w[0],$guide_w[1]]);
    			}else{
    				if($guide_w>=1000000){
    					$query->where('guide_price','>=',$guide_w);
    				}else if($guide_w<=100000){
    					$query->where('guide_price','<=',$guide_w);
    				}else{
    					return false;
    				}
    			}
    		}
            // 按服务费筛选
    		if(!empty($where['first_pay'])){
    			$first_w = $where['first_pay'];
    			if(is_array($first_w)){
    				$query->whereBetween('first_pay',[$first_w[0],$first_w[1]]);
    			}else{
    				if($first_w>=50000){
    					$query->where('first_pay','>=',$first_w);
    				}else if($first_w<=10000){
    					$query->where('first_pay','<=',$first_w);
    				}else{
    					return false;
    				}
    			}
    		}
            // 按月租金筛选
    		if(!empty($where['month_pay'])){
    			$month_w = $where['month_pay'];
    			if(is_array($month_w)){
    				$query->whereBetween('month_pay',[$month_w[0],$month_w[1]]);
    			}else{
    				if($month_w>=2000){
    					$query->where('month_pay','>=',$month_w);
    				}else if($month_w<=5000){
    					$query->where('month_pay','<=',$month_w);
    				}else{
    					return false;
    				}
    			}
    		}
            // 按照车型筛选
    		if(!empty($where['Models'])){
    			$type_w = $where['Models'];
    			$query->where('type','=',$type_w);
    		}
            // 按地区筛选
    		if(!empty($where['region_id'])){
    			$region_w = $where['region_id'];
    			$query->where('region_id','=',$region_w);
    		}
            // 搜索汽车名车或描述
            if(!empty($where['searchkey'])){
                $key_w = $where['searchkey'];
                $query->where('name','like','%'.$key_w.'%')->orWhere('details','like','%'.$key_w.'%')->orWhere('desc','like','%'.$key_w.'%');
            }

    	})
        ->where('is_show','1')
    	->orderBy($order_wa[$order_w][0],$order_wa[$order_w][1])
    	->paginate($page);
    }

    /**
     * 首页展示的数据
     */
    public function HomePage() {
        // 热销
    	$HotData = $this->where('is_hot','>','0')->where('is_show','1')->orderBy('is_hot','asc')->limit(8)->get();
        // 是否上新
    	$NewData = $this->where('is_new','>','0')->where('is_show','1')->limit(8)->get();
        // 促销中
    	$PromoData = $this->where('is_promo','>','0')->where('is_show','1')->orderBy('is_promo','asc')->limit(7)->get();
    	return ['hot'=>$HotData,'new'=>$NewData,'promo'=>$PromoData];
    }

}
